//
//  FoodTherapyViewController.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "FoodTherapyViewController.h"

@interface FoodTherapyViewController ()

@end

@implementation FoodTherapyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"对症食疗";
    //自定义tableview
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64)style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    
    //菊花类
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.labelText = @"加载中";
    [self.HUD showAnimated:YES whileExecutingBlock:^{
  //调用自定义方法
        [self doTask];
    } completionBlock:^{
        
        
    }];
    
    //初始化字典,数组
    self.myDic = [NSMutableDictionary dictionary];
    self.myArray = [NSMutableArray array];
    //AFN解析数据
    [GetAFN downLoadBlock:@"http://42.121.13.106:8080/HandheldKitchen/api/vegetable/tbloffice!getOffice.do?phonetype=2&is_traditional=0" block:^(id result) {
        //获取解析数据
        self.myDic = result;
        NSMutableArray *array = [NSMutableArray arrayWithArray:[self.myDic objectForKey:@"data"]];
            for (NSDictionary *dic in array) {
            FoodTherapy *foodTherapy = [[FoodTherapy alloc] init];
            //kvc赋值
            [foodTherapy setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:foodTherapy];
        }
        //刷新tableView
        [self.myTableView reloadData];
        //加载完成,取消菊花
        [self.HUD removeFromSuperview];
        
    }];


}
//菊花类自定义方法,设置菊花存在的时间
- (void)doTask
{
    sleep(20);
}
//返回row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    FoodTherapyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[FoodTherapyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    //初始化foodTherapy对象
    FoodTherapy *foodTherapy = [self.myArray objectAtIndex:indexPath.row];
    //sd异步请求图片
    NSString *urlStr = foodTherapy.imagePath;
    NSString *urlStrEncode = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStrEncode];
    [cell.therapyImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    [cell setFoodTherapy:foodTherapy];
    
   //系统方法,设置cell右侧箭头
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通过点击cell进入食疗详情页面
    TherapyDetailViewController *therapyDetailVC = [[TherapyDetailViewController alloc] init];
    //初始化model
    FoodTherapy *foodTherapy = [self.myArray objectAtIndex:indexPath.row];

    //传值
    therapyDetailVC.officeId = foodTherapy.officeId;
    therapyDetailVC.officeName = foodTherapy.officeName;
   //push到详情页面
    [self.navigationController pushViewController:therapyDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
